package com.chinawayltd.ews.sample.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinawayltd.ews.sample.config.InterfaceConfig;
import com.chinawayltd.ews.sample.manager.openApi.OpenApiManager;
import com.chinawayltd.ews.sample.mapper.DriverExtendMapper;
import com.chinawayltd.ews.sample.mapper.TDriverExtendMapper;
import com.chinawayltd.ews.sample.mapper.TDriverMapper;
import com.chinawayltd.ews.sample.mapper.TaskProducerMapper;
import com.chinawayltd.ews.sample.model.TDriver;
import com.chinawayltd.ews.sample.model.TDriverExample;
import com.chinawayltd.ews.sample.model.TDriverExtend;
import com.chinawayltd.ews.sample.model.openApi.DriverDTO;
import com.chinawayltd.ews.sample.service.SynchroDriverService;
import com.chinawayltd.ews.sample.util.map.MapUtils;

/**
 * @program: greeProject.timingTaskCenter
 * @description:
 * @author: xiazj
 * @create: 2018-05-25 13:17
 **/
@Service
public class SynchroDriverServiceImpl implements SynchroDriverService {

    @Autowired
    private TDriverMapper tDriverMapper;
    @Autowired
    private DriverExtendMapper driverExtendMapper;
    @Autowired
    private TDriverExtendMapper tDriverExtendMapper;
    @Autowired
    private TaskProducerMapper taskProducerMapper;
    @Autowired
    private OpenApiManager openApiManager;
    private SimpleDateFormat sdf_date_time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final SimpleDateFormat YYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
    @Override
    @Transactional
    public void synchroDriverData() throws Exception {
        List<DriverDTO> list = openApiManager.getDriverList("2015-01-01 00:00:00", InterfaceConfig.ORG_CODE);

        if(list != null && !list.isEmpty()){
            List<TDriver> driverList = tDriverMapper.selectByExample(new TDriverExample());
            Map<String, TDriver> driverMap =  MapUtils.listToMap(driverList, "g7sId");
            for (DriverDTO dto : list) {
                String driverId = dto.getId();
                if(StringUtils.isNotBlank(driverId)){
                    TDriver old = driverMap.get(driverId);
                    //修改
                    if(old != null){
                        update(dto, old);
                    }else{//新增
                        insert(dto);
                    }
                }
            }
            
        }
    }

    /**
     * 更新车辆
     */
    private void update(DriverDTO dto, TDriver mode) throws ParseException {
        String updatetime = dto.getUpdatetime();
        Date updateDate = sdf_date_time.parse(updatetime);
        if(mode.getUpdateTime().before(updateDate)){
            copyField(dto, mode);
            tDriverMapper.updateByPrimaryKey(mode);
        }
    }

    /**
     * 新增司机
     */
    private void insert(DriverDTO driver) throws ParseException {
        TDriver record = new TDriver();
        copyField(driver, record);
        record.setCreateTime(record.getUpdateTime());
        driverExtendMapper.insert(record);//需要得id
        TDriverExtend driverExtend = new TDriverExtend();
        driverExtend.setId(record.getId());
        driverExtend.setWorkNum((byte) 0);
        driverExtend.setCreateTime(record.getUpdateTime());
        driverExtend.setUpdateTime(new Date());
        tDriverExtendMapper.insert(driverExtend);
        
    }

    /**
     * 复制数据
     * @param dto
     * @param mode
     */
    private void copyField(DriverDTO dto, TDriver mode) throws ParseException {
        mode.setG7sId(dto.getId());
        mode.setDriverName(dto.getDriverName());
        mode.setWorkNo(dto.getWorkNo());
        mode.setIccard(dto.getIccard());
        mode.setOrgCode(dto.getOrgCode());
        mode.setPhone(dto.getPhone());
        mode.setTruckId(dto.getTruckId());
        mode.setBindTruckType(dto.getBindTruckType());
        mode.setUpdateTime(sdf_date_time.parse(dto.getUpdatetime()));
    }

	@Override
	public void synchroDriverWorkData() throws Exception {
		Date date = new Date();
		Calendar calendar  = new GregorianCalendar();
		calendar.setTime(date);
		String endTime = YYYYMMDD.format(calendar.getTime());
		calendar.add(Calendar.DATE, -1);
		String beginTime = YYYYMMDD.format(calendar.getTime());
		taskProducerMapper.updateInDriverWorkData(beginTime, endTime);
		taskProducerMapper.updateNotInDriverWorkData(beginTime, endTime);
	}


}
